const https = require('https'); // server端请求
const fs = require('fs');
const http = require('http');
var app = http.createServer((req, res) => {
  /*
   * 登录逻辑 https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html
   */
  if(/login/.test(req.url)) {
    // 给微信服务器发请求获取结果
    https.get(`https://api.weixin.qq.com/sns/jscode2session?appid=wx3fc9a3f9b4ce8209&secret=ce50e36aece432818e0d6c8d60a1339d&js_code=${req.url.split('code=')[1]}&grant_type=authorization_code`,function(resHandler) {
      var str = '';
      resHandler.on('data',(chunk) => {
        str += chunk;
      })
      resHandler.on('end',() => {
        console.log('weixin return data::::',str);
        res.write(str);
        res.end();
      })
    })
  }else {
    fs.readFile(__dirname+'/mock.json',function(err,content) {
      res.write(content);
      res.end();
    });
  }
});
app.listen(8999);
console.log('服务器启动')